<template>
	<view>
		<headerNav :navRightIcon="false" :navLeftIcon="false">
			<view style="position: relative;width: 100%;box-sizing: border-box;" class="relative_1 mt20 prow30 flex items-center search_box">
				<input type="text" class="sou flex-1" placeholder-class="suo" v-model="keywords"
					placeholder="输入关键词搜索" confirm-type="search" @confirm="search" />
				<image src="https://dyeego.oss-cn-beijing.aliyuncs.com/uploads/20220325/91243acc628efa1338d3d38826d22484.png"
					class="souicon" mode=""></image>
				<text @click="quxiao" style="color: #FFFFFF;font-size: 26rpx;" class="ml20">取消</text>
				<u-icon class="icon_left"  @click="back" name="arrow-left" size="20" color="#fff"></u-icon>
			</view>
		</headerNav>
		<view  class="shang_city relative_1">
			<view style="width: 94%;margin: 20rpx auto;overflow: hidden;">
				<tabNav :tabList="tabList" :selectIndex.sync="selectIndex" @handleClick="handleTabSwitch"></tabNav>
			</view>
			<view v-if="pan_status==1">
				<view style="margin-top: 20rpx;background-color: #FFFFFF;box-sizing: border-box;padding:10rpx 0 10rpx 30rpx;border-radius: 16rpx;">
					<view style="line-height: 100rpx;color: #000000;font-size: 28rpx;">
						联系人
					</view>
					<view @click="liaotian(item.client.mobile2,item.client.id,item.client.name)" style="overflow: hidden;margin-bottom: 30rpx;position: relative;" v-for="(item,index) in list" :key='index'>
						<view style="float: left;width: 23%;overflow: hidden;">
							<image :src="item.client.avatar" style="width: 112rpx;height: 112rpx;border-radius: 50%;display: block;" mode=""></image>
						</view>
						<view style="width: 77%;float: left;padding: 10rpx 30rpx 10rpx  0;box-sizing: border-box;border-bottom: 1px solid #f5f5f5;">
							<view style="color: #000000;font-size: 30rpx;font-weight: 600;line-height: 50rpx;" class="yihang">
								{{item.client.name || ''}}
							</view>
							<view style="color: #999999;font-size: 24rpx;line-height: 50rpx;" class="yihang">
								{{item.client.company || '暂无公司'}}
							</view>
						</view>

					</view>
					<view style="text-align: center;margin-top: 20rpx;">
						<image v-if="kk" :src="require('@/static/my/no-data.png')" style="width: 334rpx;"  mode="widthFix"></image>
						<!-- <view style="width: 80%;margin: 0 auto;">
							<u-divider color='#717171' bg-color='#f0f2f8' height='80' border-color='#717171' text="暂无数据" v-if="list.length==0"></u-divider>
							<u-divider color='#717171' bg-color='#f0f2f8' height='80' border-color='#717171' text="没有更多了" v-if="list.length>0 && enmpy"></u-divider>
						</view> -->
					</view>
				</view>

			</view>
			<view v-if="pan_status==2">
				<view style="margin-top: 20rpx;background-color: #FFFFFF;box-sizing: border-box;padding:10rpx 0 10rpx 30rpx;border-radius: 16rpx;">
					<view style="line-height: 100rpx;color: #000000;font-size: 28rpx;">
						群组
					</view>
					<view  @click="qunliao(item.group_id)" style="overflow: hidden;margin-bottom: 30rpx;position: relative;" v-for="(item,index) in list" :key='index'>
						<view style="float: left;width: 23%;overflow: hidden;">
							<image :src="item.avatar" style="width: 112rpx;height: 112rpx;border-radius: 50%;display: block;" mode=""></image>
						</view>
						<view style="width: 77%;float: left;padding: 10rpx 30rpx 10rpx  0;box-sizing: border-box;border-bottom: 1px solid #f5f5f5;">
							<view style="color: #000000;font-size: 30rpx;font-weight: 600;line-height: 50rpx;" class="yihang">
								{{item.name || ''}}
							</view>
							<view style="color: #999999;font-size: 24rpx;line-height: 50rpx;" class="yihang">
								{{item.introduction || ''}}
							</view>
						</view>

					</view>
					<view style="text-align: center;margin-top: 20rpx;">
						<image v-if="kk" :src="require('@/static/my/no-data.png')" style="width: 334rpx;"  mode="widthFix"></image>
						<!-- <view style="width: 80%;margin: 0 auto;">
							<u-divider color='#717171' bg-color='#f0f2f8' height='80' border-color='#717171' text="暂无数据" v-if="list.length==0"></u-divider>
							<u-divider color='#717171' bg-color='#f0f2f8' height='80' border-color='#717171' text="没有更多了" v-if="list.length>0 && enmpy"></u-divider>
						</view> -->
					</view>
				</view>
			</view>
			<view v-if="pan_status==3">
				<view style="margin-top: 20rpx;background-color: #FFFFFF;box-sizing: border-box;padding:10rpx 0 10rpx 30rpx;border-radius: 16rpx;">
					<view style="line-height: 100rpx;color: #000000;font-size: 28rpx;">
						聊天记录
					</view>
					<view @click="liaotian(item.friend.client_mobile,item.friend.client_id,item.friend.client_name)" style="overflow: hidden;margin-bottom: 30rpx;position: relative;" v-for="(item,index) in friend_list" :key='index'>
						<view style="float: left;width: 23%;overflow: hidden;">
							<image :src="item.friend.client_avatar" style="width: 112rpx;height: 112rpx;border-radius: 50%;display: block;" mode=""></image>
						</view>
						<view style="width: 77%;float: left;padding: 10rpx 30rpx 10rpx  0;box-sizing: border-box;border-bottom: 1px solid #f5f5f5;">
							<view style="color: #000000;font-size: 30rpx;font-weight: 600;line-height: 50rpx;" class="yihang">
								{{item.friend.client_name || ''}}
							</view>
							<view style="color: #999999;font-size: 24rpx;line-height: 50rpx;" class="yihang">
								{{item.count_message_num || 0}}条聊天记录
							</view>
						</view>

					</view>
					<view  @click="qunliao(item.group_id)" style="overflow: hidden;margin-bottom: 30rpx;position: relative;" v-for="(item,index) in qunzu_list" :key='index'>
						<view style="float: left;width: 23%;overflow: hidden;">
							<image :src="item.avatar" style="width: 112rpx;height: 112rpx;border-radius: 50%;display: block;" mode=""></image>
						</view>
						<view style="width: 77%;float: left;padding: 10rpx 30rpx 10rpx  0;box-sizing: border-box;border-bottom: 1px solid #f5f5f5;">
							<view style="color: #000000;font-size: 30rpx;font-weight: 600;line-height: 50rpx;" class="yihang">
								{{item.name || ''}}
							</view>
							<view style="color: #999999;font-size: 24rpx;line-height: 50rpx;" class="yihang">
								{{item.introduction || ''}}
							</view>
						</view>

					</view>
					<view style="text-align: center;margin-top: 20rpx;">
						<image v-if="qunzu_list.length==0 && friend_list.length==0 " :src="require('@/static/my/no-data.png')" style="width: 334rpx;"  mode="widthFix"></image>
						<!-- <view style="width: 80%;margin: 0 auto;">
							<u-divider color='#717171' bg-color='#f0f2f8' height='80' border-color='#717171' text="暂无数据" v-if="qunzu_list.length==0 && friend_list.length==0 "></u-divider>
							<u-divider color='#717171' bg-color='#f0f2f8' height='80' border-color='#717171' text="没有更多了" v-if="qunzu_list.length>0 || friend_list.length>0 "></u-divider>
						</view> -->
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import isAuditIos from '@/common/isAuditIos.js'

	export default{
		mixins: [isAuditIos],

		data(){
			return{
				tabList: [
					{text: '联系人', type: 'contact',index: 0},
					{text: '群组', type: 'group',index: 1},
					{text: '聊天记录', type: 'chatRecord',index: 2}
				],
    		selectIndex: 1,
				scrollTop: 0,
				navheight: 0,
				statusBarHeight: 0,
				menuHeight: 0,
				haveBack: false,
				btnHeight: 30,
				btnWidth: 70,
				isScroll:true,
				slotheight:'0',
				xiao:true,

				pan_status:1,
				keywords:'',
				page:1,
				list:[],
				kk:false,
				enmpy:false,

				friend_list:[],
				qunzu_list:[],
				bgcolor: 'transparent',
				toolcolor: 'transparent'
			}
		},
		onLoad() {

		},
		onShow() {
			this.page=1
			this.kk=false
			this.list=[]
			this.enmpy=false
			this.friend_list=[]
			this.qunzu_list=[]
			if(this.pan_status==1){
				this.sousuo_ren()
			}else if(this.pan_status==2){
				this.sousuo_qun()
			}else{
				this.sousuo_liao()
			}

		},
		onReachBottom: function() {
			console.log(222222222)
			if (this.enmpy) {
				return
			} else {
				if(this.pan_status==1){
					this.sousuo_ren()
				}else if(this.pan_status==2){
					this.sousuo_qun()
				}
			}
		},
		methods:{
			back() {
				uni.navigateBack({
					delta:1,
				})
			},
			liaotian:function(id,client_id,name){
				let aid='C2C'+id
				let that=this
				this.post('api/client/info', '', true).then(res => {
					console.log(res)
					if (res.code == 1) {
						if(res.data.is_vip==1){
							that.post('api/im/im/friend_msg',{client_id:client_id},true).then(res=>{
								if(res.code==1){
									uni.setStorageSync('friend_id',res.data.friend_id)
									uni.setStorageSync('friend_name',name)
									uni.navigateTo({
									  	url: `/im/chat/chat?conversationID=${aid}`,
									  	 // url: `/im/login/login`,
									})
								}else{
												uni.showToast({
													title: res.msg,
													icon: "none",
													duration: 1000
												})
											}
							})
						}else if(res.data.is_vip==2){
							if (that.iosUpAppStore) {
								uni.showModal({
									title: '提示',
									content: '您的会员已到期,只有会员才可以聊天,请前去续费会员！',
									success: function (res1) {
										if (res1.confirm) {

											uni.navigateTo({
												url:'../../subpages/pages/vip'
											})
										} else if (res1.cancel) {

										}
									}
								});
							}
						}else if(res.data.is_vip==0){
							if (that.iosUpAppStore) {
								uni.showModal({
									title: '提示',
									content: '您的会员已到期,只有会员才可以聊天,请前去续费会员！',
									success: function (res1) {
										if (res1.confirm) {

											uni.navigateTo({
												url:'../../subpages/pages/vip'
											})
										} else if (res1.cancel) {

										}
									}
								});
							}
						}
					}
				})




			},
			qunliao:function(id){
				let aid='GROUP'+id
				let that=this
				this.post('api/client/info', '', true).then(res => {
					console.log(res)
					if (res.code == 1) {
						that.post('api/im/im/check_group',{group_id:id},true).then(re=>{
							if(re.code==1){
								uni.setStorageSync('group_id',id)
								uni.navigateTo({
								  	url: `/im/chat/group_chat?conversationID=${aid}`,
								  	 // url: `/im/login/login`,
								})
							}else if(re.code==211){
								if (that.iosUpAppStore) {
									uni.showModal({
										title: '提示',
										content: re.msg,
										success: function(res1) {
											if (res1.confirm) {
												uni.navigateTo({
													url: '../../subpages/pages/vip'
												})
											} else if (res1.cancel) {

											}
										}
									});
								}
							}else{
								uni.showToast({
									title: re.msg,
									icon: "none",
									duration: 1000
								})
							}
						})
						return
						if(res.data.is_vip==1){
							uni.setStorageSync('group_id',id)
							uni.navigateTo({
							  	url: `/im/chat/group_chat?conversationID=${aid}`,
							  	 // url: `/im/login/login`,
							})
						}else if(res.data.is_vip==2){
							uni.showModal({
								title: '提示',
								content: '您的会员已到期,只有会员才可以聊天,请前去续费会员！',
								success: function (res1) {
									if (res1.confirm) {
										uni.navigateTo({
											url:'../../subpages/pages/vip'
										})
									} else if (res1.cancel) {

									}
								}
							});
						}else if(res.data.is_vip==0){
							uni.showModal({
								title: '提示',
								content: '只有会员才可以聊天,请前去开通会员！',
								success: function (res1) {
									if (res1.confirm) {
										uni.navigateTo({
											url:'../../subpages/pages/vip'
										})
									} else if (res1.cancel) {

									}
								}
							});
						}
					}
				})




			},
			getTool:function(){
				uni.navigateBack({
					delta:1
				})
			},
			handleTabSwitch:function(index){
				this.pan_status=index
				this.page=1
				this.kk=false
				this.list=[]
				this.enmpy=false
				this.friend_list=[]
				this.qunzu_list=[]
				if(this.pan_status==1){
					this.sousuo_ren()
				}else if(this.pan_status==2){
					this.sousuo_qun()
				}else{
					this.sousuo_liao()
				}
			},
			quxiao:function(){
				this.keywords=''
				this.page=1
				this.kk=false
				this.list=[]
				this.enmpy=false
				this.friend_list=[]
				this.qunzu_list=[]
				if(this.pan_status==1){
					this.sousuo_ren()
				}else if(this.pan_status==2){
					this.sousuo_qun()
				}else{
					this.sousuo_liao()
				}
			},
			search:function(){
				this.page=1
				this.kk=false
				this.list=[]
				this.enmpy=false
				this.friend_list=[]
				this.qunzu_list=[]
				if(this.pan_status==1){
					this.sousuo_ren()
				}else if(this.pan_status==2){
					this.sousuo_qun()
				}else{
					this.sousuo_liao()
				}
			},
			getSysHeightInfo() {
				const that = this
				let statusBarHeight, menuButtonObject, menuHeight, navheight, haveBack, btnHeight, btnWidth

				statusBarHeight = uni.getSystemInfoSync().statusBarHeight
				//#ifdef MP-WEIXIN
				menuButtonObject = uni.getMenuButtonBoundingClientRect()
				menuHeight = menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2
				navheight = menuHeight + statusBarHeight
				btnHeight = menuButtonObject.height
				btnWidth = menuButtonObject.width
				//#endif
				//#ifdef H5
				menuHeight = 44
				navheight = menuHeight + statusBarHeight
				btnHeight = 30
				btnWidth = 70
				//#endif
				if (getCurrentPages().length === 1) { // 当只有一个页面时
					haveBack = false;
				} else {
					haveBack = true;
				}
				that.navheight = navheight
				that.statusBarHeight = statusBarHeight
				that.menuHeight = menuHeight
				that.haveBack = haveBack
			},


			// 搜索联系人
			sousuo_ren:function(){
				this.post('api/im/im/search_contact',{keywords:this.keywords,page:this.page,limit:10},true).then(res=>{
					console.log(res)
					if(res.code==1){
						if(this.page==1){
							if (res.data.data== null || res.data.data.length == 0) {
								this.kk = true
								this.enmpy = true
								return
							} else if (res.data.data.length < 15) {
								this.enmpy = true
							}
						}else{
							if (res.data.data== null || res.data.data.length == 0) {
								this.enmpy = true
								return
							} else if (res.data.data.length < 15) {
								this.enmpy = true
							}
						}

						this.page++
						this.list.push(...res.data.data)
					}else{
						this.kk = true
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 1000
						})
					}
				})
			},
			// 搜索群组
			sousuo_qun:function(){
				this.post('api/im/im/search_group',{keywords:this.keywords,page:this.page,limit:10},true).then(res=>{
					console.log(res)
					if(res.code==1){
						if(this.page==1){
							if (res.data.data== null || res.data.data.length == 0) {
								this.kk = true
								this.enmpy = true
								return
							} else if (res.data.data.length < 15) {
								this.enmpy = true
							}
						}else{
							if (res.data.data== null || res.data.data.length == 0) {
								this.enmpy = true
								return
							} else if (res.data.data.length < 15) {
								this.enmpy = true
							}
						}

						this.page++
						this.list.push(...res.data.data)
					}else{
						this.kk = true
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 1000
						})
					}
				})
			},
			// 搜索群组
			sousuo_liao:function(){
				this.post('api/im/im/search_message',{keywords:this.keywords},true).then(res=>{
					console.log(res)
					if(res.code==1){
						this.friend_list=res.data.friend_message
						this.qunzu_list=res.data.group_message
					}
				})
			},
		},
		mounted() {
			const that = this
			this.getSysHeightInfo()
			//#ifdef H5
			window.onscroll = function() {
				var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
				if (scrollTop > 30) {
					that.isScroll = true
				} else {
					that.isScroll = false
				}
			}
			//#endif

		},
		onPageScroll(t) {

			t.scrollTop > 20 ? this.xiao = false : this.xiao = true;
			// t.scrollTop > 80 ? this.da = true : this.da = false;

		},
		watch: {
			scrollTop(newValue, oldValue) {
				//#ifdef MP-WEIXIN
				const that = this
				if (newValue > 1) {
					that.isScroll = true
				} else {
					that.isScroll = false
				}
				//#endif
			}
		}
	}
</script>

<style lang="scss">
	.bg{
		width: 100%;
	}
	page{
		background-color: #F8F8F8!important;
		font-family: Demibold;
	}
	.sou {
		height: 80rpx;
		line-height: 80rpx;
		border-radius: 40rpx;
		color: #FFFFFF;
		font-size: 26rpx;
		box-sizing: border-box;
		padding: 0 30rpx 0 80rpx;
		background-color: rgba(255,255,255,.3);
	}
	.souicon {
		width: 30rpx;
		height: 30rpx;
		position: absolute;
		top: 50%;
		left: 130rpx;
		transform: translateY(-50%);

	}
	.suo {
		color: #FFFFFF;
		opacity: 1;
		font-size: 26rpx;
	}
	.shang_city{
		width: 92%;
		margin: 0 auto;
		box-sizing: border-box;
		padding-bottom: 200rpx;
	}
	.pan_active{
		color: #fff;
		font-size: 36rpx;
		opacity: 1;
	}
	.pan_as{
		color: #fff;
		font-size:28rpx;
		opacity: 0.6;
	}
	.pan_active::after {
		content: "";
		position: absolute;
		left: 50%;
		bottom:0rpx;
		right: 0;
		width: 60rpx;
		height: 6rpx;
		border-radius: 3rpx;
		background: #FFFFFF;
		transform: translateX(-50%);
	}
	.hai{
		text-align: center;
		font-size: 30rpx;
		color: #FFFFFF;
	}







	// 头部
	.navbar {
		width: 100%;
		overflow: hidden;
		top: 0;
		left: 0;
		z-index: 999;
		flex-shrink: 0;
		color: #fff;
		font-size: 16px;
	}

	.nav-normal {
		position: relative;
	}

	.nav-fixed {
		position: fixed;
	}

	.logo-centent {
		display: flex;
		align-items: center;
	}

	.logo-centent image {
		width: 260rpx;
		height: 68rpx;
		margin-left: 20rpx;
	}

	.hearder-status {
		display: flex;
		align-items: center;
		position: absolute;
		padding-left: 30rpx;
		height: 100%;
		z-index: 99999;

	}

	.header-title {
		height: 100%;
		left: 0;
		right: 0;
		position: absolute;
		margin: auto;
		text-align: center;
	}
	.header-title image{
		position: absolute;
		width:270rpx;
		height: 35rpx;
		top: 50%;
		left: 50%;
		transform: translate(-50%,-50%);
	}

	.header-title-left {
		height: 100%;
		left: 0;
		right: 0;
		position: absolute;
		margin: auto;
		text-align: left;
	}

	.back-text {
		margin-left: 7px;
		width: 100upx;
		text-overflow: ellipsis;
		overflow: hidden;
		white-space: nowrap;
	}

	.hearder-status .nav-title-left {
		display: flex;
		justify-content: space-between;
		align-items: center;
		color: rgba(0, 0, 0, .8);
		width: 100%;
		font-weight: bold;
	}
	.hearder-status .nav-btn {
		/* background-color: rgba(255, 255, 255, .5);
		border-radius: 15px; */
		display: flex;
		justify-content: space-between;
		align-items: center;
		color: rgba(0, 0, 0, .8);
		width: 50%;
		font-weight: bold;
	}

	.hearder-status .nav-btn .gotback,
	.hearder-status .nav-btn .gothome {
		flex: 1;
		text-align: center;
		font-size: 18px;
	}
	.Addslot {
		position: absolute;
		width: 100%;
	}
	.icon_left {
		position: absolute;
		top: 20rpx;
		left: 24rpx;
		z-index: 99999;
	}
	.search_box {
		padding-left: 100rpx;
	}
</style>